package com.alogic.gw;

import com.alogic.remote.Request;
import com.alogic.remote.Response;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.Context;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 基于会话的GatewayHandler
 *
 * 本handler会通过http头向后端传递
 *
 * @since 1.6.12.41 [20190902] <br>
 */
public class SessionGatewayHandler extends GatewayHandler{
    protected String $operator = "$user";
    protected String $provider = "$provider";
    protected String $session = "$session";

    @Override
    protected Response doExecute(String endpointPath,
                                 HttpServletRequest request, Request req, Context ctx)
            throws IOException {
        String provider = PropertiesConstants.getString(ctx, $provider, "");
        if (StringUtils.isNotEmpty(provider)) {
            req.setHeader("x-alogic-provider", provider);
        }
        String operator = PropertiesConstants.getString(ctx, $operator, "");
        if (StringUtils.isNotEmpty(operator)) {
            req.setHeader("x-alogic-operator", operator);
        }
        String session = PropertiesConstants.getString(ctx, $session, "");
        if (StringUtils.isNotEmpty(session)){
            req.setHeader("x-alogic-session", session);
        }

        return req.execute(endpointPath, ctx.getGlobalSerial(), ctx);
    }
}
